// javamaps
// Copyright (c) 2010 Lup Gratian
package Rendering.Renderers;
import Core.*;
import Rendering.*;
import java.awt.Graphics2D;
import java.awt.image.VolatileImage;
import java.util.HashMap;
import java.util.Iterator;

public class OverlayRenderer implements IRenderer, IOverlayHost {
    private IRendererHost host_;
    HashMap<Object, IVisual> overlays_;

    public OverlayRenderer(IRendererHost host) {
        host_ = host;
        overlays_ = new HashMap<Object, IVisual>(8);
    }

    /*
     * Metode publice.
     */
    public boolean IsEditor() { return false; }
    public ILayer Layer() { return null; }

    public int ZIndex() { return Integer.MAX_VALUE - 1; } // Sub stratul de controale.
    public void SetZIndex(int value) {
        throw new UnsupportedOperationException("ZIndex of image layer cannot be changed.");
    }

    public boolean HasPrefetcher() { return false; }
    public IPrefetcher Prefetcher() { return null; }
    public boolean MouseDown(Point point, View view, Modifier modifier) { return false; }
    public boolean MouseUp(Point point, View view, Modifier modifier) { return false; }
    public boolean MouseMoved(Point point, View view, Modifier modifier) { return false; }
    public boolean MouseDragged(Point point, View view, Modifier modifier) { return false; }
    public boolean MouseCaptured() { return false; } // Nu poate captura mouse-ul.
    public IObjectInfo HitTest(Point point, View view) { return null; }
    public double Opacity() { return 1.0; } // Intotdeauna opac.
    public void SetOpacity(double value) {}
    public boolean Visible() { return true; } // Nu poate fi ascuns.
    public void SetVisible(boolean value) {}

    public void Render(View view) {
        // Deseneaza toate obiectele.
        VolatileImage buffer = view.GetBuffer(this);
        Graphics2D g = buffer.createGraphics();

        if(overlays_.size() == 0) {
            // Nimic de desenat.
            g.dispose();
            return;
        }

        Iterator<IVisual> overlayIt = overlays_.values().iterator();
        while(overlayIt.hasNext()) {
            IVisual overlay = overlayIt.next();
            overlay.Draw(g, view);
        }

        g.dispose();
    }

    public void AddOverlay(Object key, IVisual item) {
        overlays_.put(key, item);
    }

    public void RemoveOverlay(Object key) {
        overlays_.remove(key);
        host_.Repaint();
    }

    public boolean HasOverlay(Object key) {
        return overlays_.containsKey(key);
    }

    public IVisual GetOverlay(Object key) {
        return overlays_.get(key);
    }
}